<?php
	if(!defined('BASEPATH')) exit('No direct script access allowed');
	/**
	*  全局通用函数
	*/
	

	/*
	*  消息
	*  @message string 指定显示的主要消息
	*  @href array 附加的跳转链接 键值分别代表文本和地址
	*  @type string 特定的字符串，决定消息的图标 success:普通消息 warning:警示 
	*  @timeout 自动跳转时间
	*/
	function showMessage($message = 'no message!',$href=array(),$type='success',$timeout=3){
		$CI = & get_instance();
		//$data = func_get_args()
		switch($type){
			case 'warning' :$status='warning';break;
			case 'error' :$status='danger';break;
			case 'info' :$status='info';break;
			case 'success' :$status='ok';break;
			default :$status='info';break;
		}
		if($href){
			foreach($href as $k=>$v){
				$href[$k] = site_url($v);
			}
		}
		$data = array(
			'message' => $message,
			'href' => $href,
			'type' => $type,
			'status' => $status,
			'timeout' => $timeout
		);
		
		
		//刷新地址
		if($backrefresh = $CI->session->userdata('backrefresh')){
			$data['backrefresh'] = site_url($backrefresh);
			$CI->session->unset_userdata('backrefresh');
			
		}
		else if($crefresh = $CI->session->userdata('crefresh')){
			$data['crefresh'] = site_url($crefresh);
			$CI->session->unset_userdata('crefresh');
		}
		//跳转连接
	
		if($location = $CI->session->userdata('location')){
			$data['location'] = site_url($location);
			$CI->session->unset_userdata('location');
		}
		

		ob_start();
			$CI->load->view('templates/header');
			$CI->load->view('templates/message',$data);
			$CI->load->view('templates/footer');
			
			$buffer = ob_get_contents();
		ob_end_clean();
	
		echo  $buffer;
		exit;
		
	}
	
	
	/*
	* 生成并设置验证码
	* @pre_captcha  存储的验证码相关session键名
	*/
	function captcha($pre_captcha = 'captcha',$width = false, $height = false){
		$CI = & get_instance();
		$CI->load->helper('captcha');
		
		$captcha_path = './data/captcha/';
		
		if(!file_exists($captcha_path)){
			mkdir($captcha_path,0555);
		}
		$data = array(
			'img_path' => $captcha_path,
			'img_url' => base_url().$captcha_path,
			'width' => '100',
			'height' => '30',
			'expiration' => 7200

		);
		if($width){
			$data['img_width'] = $width;
		}
		if($height){
			$data['img_height'] = $height;
		}
			
		$cap = create_captcha($data);
		$session = array(
			'word' => $cap['word'],
			'captcha_time' => $cap['time']
		);
		set_session($pre_captcha,$session);
		
		return  $cap;
		
	}
	
	if(!function_exists('encryptUserPassword')){

		//加盐加密用户密码
		function encryptUserPassword($password){
			$CI = & get_instance();
			$p1 = md5($password).$CI->config->item('salt');
			return md5($p1);
		}
	}
	
	if(!function_exists('downloadFile')){
		//下载文件,如果是下载已经存在的文件，则直接$data = file_get_contents($filepath)读取到文件的内容
		function downloadFile($name,$data){
			$CI = & get_instance();
			$CI->load->helper('download');
			force_download($name, $data); 
		}
	}
	

	//前端错误面板
	function error($error){
		?>
		  <div class="alert alert-warning alert-dismissable">
		  <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
		  <?php echo $error;?>
		  </div>
		<?php
	}
	
	//用于快速设定session
	function set_session($key,$value=''){
		$CI = & get_instance();
		$CI->load->library('session');
		
		$CI->session->set_userdata(array($key=>$value));
	}
	
	//获取当前在线的用户信息
	function guser($name){
		$CI = & get_instance();
		$session = $CI->session->userdata('user');
		return  $session[$name];
	}
	
	//设置闪存
	function set_flash($key,$value=''){
		$CI = & get_instance();
		$CI->load->library('session');
		
		$CI->session->set_flashdata($key,$value);
	}
	
	//设置页面头部内容
	function meta($meta){
		$c = "";
		switch($meta){
			case 'title': $c = "我品牌书";break;
			case 'keywords': $c =  "";break;
			case 'description': $c = "";break;
		}
		return $c;
	}
	
	//发送邮件
	function sendEmail($from,$to,$message = '',$subject = '',$protocol = 'mail',$reply_to = false,$cc = false,$bcc = false){
		
		$CI = & get_instance();
		$CI->load->library('email');
		
		$config = array(
			'protocol' => $protocol
		);
		
		/*smtp的部分设置*/
        $config['smtp_host'] = 'ssl://smtp.qq.com';
        $config['smtp_user'] = '726331595@qq.com';
        $config['smtp_pass'] = '5gbytl@sina.cn';
        $config['smtp_port'] = '465';
        $config['charset'] = 'utf-8';
        $config['crlf'] = "\r\n";
        $config['newline'] = "\r\n";
        $config['mailtype'] = "html";
		
        $CI->email->initialize($config);

		$CI->email->from($from);
		$CI->email->to($to);
		
		if($reply_to){
			$CI->email->reply_to($reply_to);
		}
		if($cc){
			$CI->email->cc($cc);
		}
		if($subject){
			$CI->email->bcc($bcc);
		}
		$CI->email->message($message);
		
		if($subject){
			$CI->email->subject($subject);
		}
		
		return $CI->email->send();
		//echo $CI->email->print_debugger();

	}

?>